--[[ 		  SLDataText Module: ItemTracker 			]]
--[[ Author: Taffu  RevDate: 06/27/2012  Version: 1.0.3 ]]

local SLDT, MODNAME = SLDataText, "ItemTracker"
if ( SLDT ) then SLDT.ItemTracker = CreateFrame("Frame") end
local db, frame, text, tool, popup
local diaBG = { bgFile = "Interface\\DialogFrame\\UI-DialogBox-Background", edgeFile = "Interface\\DialogFrame\\UI-DialogBox-Border", tile = true, tileSize = 16, edgeSize = 16, insets = { left = 4, right = 4, top = 4, bottom = 4 } }
local pulBG = { bgFile = "Interface\\DialogFrame\\UI-Tooltips-Background", tile = true, tileSize = 16, insets = { left = 0, top = 0, right = 0, bottom = 0 } }

local function CreatePopupMenu()
	local popup = CreateFrame("Frame", nil, frame)
	popup:SetBackdrop(diaBG)
	popup:SetWidth(200); popup:SetHeight(118)
	popup:ClearAllPoints()
	
	popup.header = popup:CreateFontString(nil, "OVERLAY", "GameFontNormal")
	popup.header:SetFont(popup.header:GetFont(), 14)
	popup.header:SetText("Item Tracker")
	popup.header:ClearAllPoints()
	popup.header:SetPoint("TOPLEFT", popup, "TOPLEFT", 10, -10)
	
	popup.spacer = popup:CreateTexture()
	popup.spacer:SetTexture(1,1,0.5,0.75)
	popup.spacer:SetWidth(155); popup.spacer:SetHeight(1)
	popup.spacer:ClearAllPoints()
	popup.spacer:SetPoint("TOPLEFT", popup.header, "BOTTOMLEFT", 0, -4)
	
	popup.dir = popup:CreateFontString(nil, "OVERLAY", "GameFontNormal")
	popup.dir:SetFont(popup.dir:GetFont(), 10)
	popup.dir:SetTextColor(1,1,1)
	popup.dir:SetWordWrap(true)
	popup.dir:SetText("Enter Item Name or ID to track:")
	popup.dir:ClearAllPoints()
	popup.dir:SetPoint("TOPLEFT", popup.spacer, "BOTTOMLEFT", 0, -6)
	
	popup.item = CreateFrame("EditBox", "SLDT_ItemTrackerPopupItemField", popup, "InputBoxTemplate")
	popup.item:SetFontObject("ChatFontNormal")
	popup.item:SetTextInsets(0, 0, 3, 3)
	popup.item:SetWidth(175); popup.item:SetHeight(18)
	popup.item:SetAutoFocus(false)
	popup.item:SetMaxLetters(254)
	
	popup.item:SetScript("OnEnterPressed" , function(self)
		db.tracking = self:GetText()
		self:SetText(db.tracking or "")
		self:ClearFocus()
		SLDT.ItemTracker:Refresh()
		PlaySound("GAMEGENERICBUTTONPRESS")
	end)
	popup.item:SetScript("OnEscapePressed", function(self)
		self:SetText(db.tracking or "")
		self:ClearFocus()
	end)
	popup.item:SetText(db.tracking or "")
	popup.item:ClearAllPoints()
	popup.item:SetPoint("TOPLEFT", popup.dir, "BOTTOMLEFT", 4, -4)
	
	popup.dir2 = popup:CreateFontString(nil, "OVERLAY", "GameFontNormal")
	popup.dir2:SetFont(popup.dir2:GetFont(), 10)
	popup.dir2:SetTextColor(1,1,1)
	popup.dir2:SetWordWrap(true)
	popup.dir2:SetText("Enter goal amount (optional):")
	popup.dir2:ClearAllPoints()
	popup.dir2:SetPoint("TOPLEFT", popup.item, "BOTTOMLEFT", -4, -6)
	
	popup.box = CreateFrame("EditBox", "SLDT_ItemTrackerPopupGoalField", popup, "InputBoxTemplate")
	popup.box:SetFontObject("ChatFontNormal")
	popup.box:SetTextInsets(0, 0, 3, 3)
	popup.box:SetWidth(175); popup.box:SetHeight(18)
	popup.box:SetAutoFocus(false)
	popup.box:SetMaxLetters(254)
	
	popup.box:SetScript("OnEnterPressed" , function(self)
		db.goal = self:GetText() ~= "" and tonumber(self:GetText()) or 0
		self:SetText(db.goal)
		self:ClearFocus()
		SLDT.ItemTracker:Refresh()
		PlaySound("GAMEGENERICBUTTONPRESS")
	end)
	popup.box:SetScript("OnEscapePressed", function(self)
		self:SetText(db.goal or "")
		self:ClearFocus()
	end)
	popup.box:SetText(db.goal)
	popup.box:ClearAllPoints()
	popup.box:SetPoint("TOPLEFT", popup.dir2, "BOTTOMLEFT", 4, -4)
	
	tool:SetScript("OnMouseDown", function(this, button)
		if ( button == "LeftButton" ) then
			if ( popup:IsShown() ) then				
				PlaySound("GAMEGENERICBUTTONPRESS")
				popup:Hide()
			else
				local _, _, relPt, _, _ = frame:GetPoint()
				if ( relPt == "TOPLEFT" or relPt == "TOP" or relPt == "TOPRIGHT" ) then
					popup:ClearAllPoints()
					popup:SetPoint("TOP", frame, "BOTTOM", 0, 0)
				else
					popup:ClearAllPoints()
					popup:SetPoint("BOTTOM", frame, "TOP", 0, 0)
				end
				PlaySound("GAMEGENERICBUTTONPRESS")
				popup:Show()
			end
		end
	end)
	
	popup:Hide()
end

function SLDT.ItemTracker:Enable()
	if ( db.enabled ) then
		self:RegisterEvent("BAG_UPDATE")
		self:SetScript("OnEvent", function() self:Refresh() end)
	end
	self:Refresh()
end

function SLDT.ItemTracker:Disable()
	if ( not db.enabled ) then
		self:UnregisterEvent("BAG_UPDATE")
	end
	self:Refresh()
end

function SLDT.ItemTracker:Refresh()
	if ( db.enabled or SLDataText.db.profile.configMode ) then
		if ( not self.firstRun ) then self.firstRun = true; SLDT:UpdateBaseText(self, db) end
		
		local itemCount, itemName, itemTexture = 0, "", ""
		for container = 0, NUM_BAG_SLOTS do
			for slot = 0, GetContainerNumSlots(container) do
				local itemID = GetContainerItemID(container, slot)
				if ( itemID ) then
					local name, link, quality, iLevel, reqLevel, class, subclass, maxStack, equipSlot, texture, vendorPrice = GetItemInfo(itemID)
					if ( name == db.tracking or itemID == db.tracking ) then
						itemCount = GetItemCount(itemID)
						itemName = name; itemTexture = texture
						
						if ( db.goal ~= 0 and itemCount >= db.goal ) then SLDT:Pulse(self, true) else SLDT:Pulse(self, false) end
					else
						itemName = db.tracking
					end
				end
			end
		end
		
		local txstr = nil
		if ( itemTexture and itemTexture ~= "" ) then txstr = string.format("|T%s:0|t ", itemTexture) end
		
		if ( db.truncate and db.tracking ~= "" and itemName ) then
			if ( string.len(itemName) > db.trunThresh ) then
				local newStr = string.sub(itemName, 1, db.trunThresh)
				itemName = string.format("%s", newStr)
			end
		end
		
		text:SetFormattedText("%s|cff%s%s|r%s", txstr or "", SLDT.db.profile.cCol and SLDT.classColor or "ffffff", db.tracking ~= "" and itemName..": " or "ItemTracker", db.tracking ~= "" and itemCount or "")
		SLDT:UpdateBaseFrame(SLDT.ItemTracker, db)
	else
		if ( frame:IsShown() and not SLDataText.db.profile.configMode ) then frame:Hide() end
	end
end

SLDT.ItemTracker.optsTbl = {
	[1] = { [1] = "toggle", [2] = "Enabled", [3] = "enabled" },
	[2] = { [1] = "toggle", [2] = "Global Font", [3] = "gfont" },
	[3] = { [1] = "toggle", [2] = "Outline", [3] = "outline" },
	[4] = { [1] = "toggle", [2] = "Force Shown", [3] = "forceShow" },
	[5] = { [1] = "toggle", [2] = "Tooltip On", [3] = "tooltipOn" },
	[6] = { [1] = "toggle", [2] = "Show Icon", [3] = "showIcon" },
	[7] = { [1] = "toggle", [2] = "Truncate Name", [3] = "truncate" },
	[8] = { [1] = "range", [2] = "Truncate Threshhold", [3] = "trunThresh", [4] = 8, [5] = 128, [6] = 1 },
	[9] = { [1] = "text", [2] = "Item Tracked", [3] = "tracking" },
	[10] = { [1] = "range", [2] = "Font Size", [3] = "fontSize", [4] = 6, [5] = 40, [6] = 1 },
	[11] = { [1] = "select", [2] = "Font", [3] = "font", [4] = SLDT.fontTbl },
	[12] = { [1] = "select", [2] = "Justify", [3] = "aP", [4] = SLDT.justTbl },
	[13] = { [1] = "text", [2] = "Parent", [3] = "anch" },
	[14] = { [1] = "select", [2] = "Anchor", [3] = "aF", [4] = SLDT.anchTbl },
	[15] = { [1] = "text", [2] = "X Offset", [3] = "xOff" },
	[16] = { [1] = "text", [2] = "Y Offset", [3] = "yOff" },
	[17] = { [1] = "select", [2] = "Frame Strata", [3] = "strata", [4] = SLDT.stratTbl },
}

local function OnInit()
	SLDT.ItemTracker.db = SLDT.db:RegisterNamespace(MODNAME)
    SLDT.ItemTracker.db:RegisterDefaults({
        profile = {
			name = "ItemTracker",
			enabled = true,
			tracking = "",
			goal = 0,
			aP = "CENTER",
			anch = "UIParent",
			aF = "CENTER",
			xOff = 0,
			yOff = -72,
			strata = "LOW",
			gfont = false,
			fontSize = 12,
			font = "Arial Narrow",
			outline = false,
			forceShow = false,
			tooltipOn = true,
			showIcon = true,
			truncate = true,
			trunThresh = 10,
        },
    })
	db = SLDT.ItemTracker.db.profile
	
	SLDT:AddModule(MODNAME, db)
	frame, text, tool = SLDT:SetupBaseFrame(SLDT.ItemTracker)
	CreatePopupMenu()
	
	SLDT.ItemTracker:UnregisterEvent("PLAYER_ENTERING_WORLD")
	SLDT.ItemTracker:Enable()	
end

if ( IsAddOnLoaded("SLDataText") ) then
	SLDT.ItemTracker:RegisterEvent("PLAYER_ENTERING_WORLD")
	SLDT.ItemTracker:SetScript("OnEvent", OnInit)
end